var express = require("express");
var router = express.Router();
const multer = require("multer");
const { formatResponse, analysisToken, uploading } = require("../utils/tool");
const { ValidationError, UploadError } = require("../utils/errors");

router.post("/", async function (req, res, next) {
  // single 方法里面书写上传控件的name值
  // uploading.single("file")(req, res, function (err) {
  //   if (err instanceof multer.MulterError) {
  //     next(new UploadError("上传文件失败,请检查文件的大小,控制在2MB大小以内"));
  //   } else {
  //     console.log(req.file);
  //     const path = "/static/uploads/" + req.file.filename;
  //     res.send(formatResponse(0, "", path));
  //   }
  // });
  uploading.single("file")(req, res, function (err) {
    if (err instanceof multer.MulterError) {
      next(new UploadError("上传文件失败，请检查文件的大小，控制在 2MB 以内"));
    } else {
      const path = "/static/uploads/" + req.file.filename;
      res.send(formatResponse(0, "", path));
    }
  });
});

module.exports = router;
